//
//  WPExceptionalVC.m
//  wobingwoyi
//
//  Created by WGP on 2016/12/23.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPExceptionalVC.h"
#import "WPMRModel.h"
#import "WPCoinTopUp.h"

#define kMaxLength 18
@interface WPExceptionalVC ()<UITextFieldDelegate>

@property(nonatomic,strong)UIScrollView *bgScrollView;
@property(nonatomic,strong)UIImageView *iconView;
@property(nonatomic,strong)UILabel *nameLabel;
@property(nonatomic,strong)WPBtnNoHighLight *currentSelectedBtn;
@property(nonatomic,strong)UILabel *currentYPLabel;
@property(nonatomic,strong)WPBtnNoHighLight *exceptionalBtn4;
@property(nonatomic,strong)UIButton *rewardImmediatelyBtn;
@property(nonatomic,assign)NSInteger rewardYPNum;
@property(nonatomic,assign)int currentYPNum;
@property(nonatomic,strong)UITextField *textField;
@property(nonatomic,strong)UIButton *dashanBtn;

@property(nonatomic,assign)CGRect oldFrame;
@property(nonatomic,strong)UIButton *coverBtn;
@property(nonatomic,strong)UIView *customYPView;

@end

@implementation WPExceptionalVC

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = RGB_SAME(250);
    
    //注册通知,监听键盘弹出事件
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
    //增加监听，当键退出时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    [self setupUI];
    
    //获取当前医票数量
    [self loadUserCoinInfomation];
}

-(void)setupUI
{
    UIImage *image = [UIImage imageNamed:@"exceptionalVC_top"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [self.view addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(image.size.height);
    }];
    
    UIView *navView = [UIView new];
    navView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:navView];
    [navView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(44);
        make.top.equalTo(self.view).offset(20);
    }];
    
    UILabel *label = [UILabel new];
    label.text = @"病例答谢";
    label.font = [UIFont systemFontOfSize:18];
    label.textColor = [UIColor whiteColor];
    [navView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(navView);
    }];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [navView addSubview:backBtn];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back_white_big"] forState:UIControlStateNormal];
//    [backBtn setBackgroundImage:[UIImage imageNamed:@"back_highlight"] forState:UIControlStateHighlighted];
    [backBtn addTarget:self action:@selector(backBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(navView);
        make.left.equalTo(navView).offset(10);
    }];
    
    [self iconView];
    [self nameLabel];
    
    UILabel *tipLabel = [UILabel new];
    tipLabel.text = @"感谢提供有效的病例!!";
    tipLabel.textColor = [UIColor colorWithHexString:@"#666666"];
    tipLabel.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:tipLabel];
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.nameLabel.mas_bottom).offset(15);
    }];
    
    [self setupBtnsWith:tipLabel];
    [self currentYPLabel];
    [self rewardImmediatelyBtn];
}

-(UIImageView *)iconView
{
    if (_iconView == nil)
    {
        _iconView = [[UIImageView alloc] init];
        [_iconView sd_setImageWithURL:[NSURL URLWithString:self.model.userImage] placeholderImage:[UIImage imageNamed:@"touxiang3"]];
        _iconView.layer.cornerRadius = 30;
        _iconView.layer.masksToBounds = YES;
        
        [self.view addSubview:_iconView];
        [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.equalTo(self.view);
            make.width.height.mas_equalTo(60);
            make.top.equalTo(self.view).offset(98*K_SCALE_FULL);
        }];
    }
    return _iconView;
}

-(UILabel *)nameLabel
{
    if (_nameLabel == nil)
    {
        _nameLabel = [UILabel new];
        _nameLabel.text = self.model.userName;
        _nameLabel.textColor = [UIColor colorWithHexString:@"#333333"];
        _nameLabel.font = [UIFont systemFontOfSize:15];
        [self.view addSubview:_nameLabel];
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.top.equalTo(self.iconView.mas_bottom).offset(15);
        }];
    }
    return _nameLabel;
}

-(void)setupBtnsWith:(UILabel *)tipLabel
{
    UIImage *image = [UIImage imageNamed:@"exceptionalBtn_selected"];
    CGFloat btnWidth = (SCREEN_WIDTH-15*2-25*2)/3.0;
    CGFloat btnHeight= btnWidth*image.size.height/image.size.width;
    
    WPBtnNoHighLight *exceptionalBtn1 = [self btnWith:5 title:nil isLast:NO];
    exceptionalBtn1.selected = YES;
    self.currentSelectedBtn = exceptionalBtn1;
    self.rewardYPNum = 5;
    
    WPBtnNoHighLight *exceptionalBtn2 = [self btnWith:10 title:nil isLast:NO];
    WPBtnNoHighLight *exceptionalBtn3 = [self btnWith:15 title:nil isLast:NO];
    WPBtnNoHighLight *exceptionalBtn4 = [self btnWith:20 title:nil isLast:NO];
    WPBtnNoHighLight *exceptionalBtn5 = [self btnWith:50 title:nil isLast:NO];
    WPBtnNoHighLight *exceptionalBtn6 = [self btnWith:0 title:@"其它医票" isLast:YES];
    
    self.exceptionalBtn4 = exceptionalBtn4;
    
    exceptionalBtn1.tag = 1000;
    exceptionalBtn2.tag = 1001;
    exceptionalBtn3.tag = 1002;
    exceptionalBtn4.tag = 1003;
    exceptionalBtn5.tag = 1004;
    exceptionalBtn6.tag = 1005;
    
    [exceptionalBtn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(25);
        make.top.equalTo(tipLabel.mas_bottom).offset(42*K_SCALE_FULL);
        make.width.mas_equalTo(btnWidth);
        make.height.mas_equalTo(btnHeight);
    }];
    [exceptionalBtn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(exceptionalBtn1.mas_right).offset(15);
        make.centerY.width.height.equalTo(exceptionalBtn1);
    }];
    [exceptionalBtn3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(exceptionalBtn2.mas_right).offset(15);
        make.centerY.width.height.equalTo(exceptionalBtn1);
    }];
    
    [exceptionalBtn4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.height.equalTo(exceptionalBtn1);
        make.top.equalTo(exceptionalBtn1.mas_bottom).offset(20);
    }];
    [exceptionalBtn5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(exceptionalBtn4.mas_right).offset(15);
        make.centerY.width.height.equalTo(exceptionalBtn4);
    }];
    [exceptionalBtn6 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(exceptionalBtn5.mas_right).offset(15);
        make.centerY.width.height.equalTo(exceptionalBtn4);
    }];
}

-(UILabel *)currentYPLabel
{
    if (_currentYPLabel == nil)
    {
        _currentYPLabel = [UILabel new];
        [self.view addSubview:_currentYPLabel];
        
        NSString *str = [NSString stringWithFormat:@"我的医票:  %zd",@"0"];
        NSRange range1 = [str rangeOfString:@"0"];
        NSRange range2 = [str rangeOfString:@"我的医票:"];
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:str];
        [attStr addAttribute:NSForegroundColorAttributeName value:GlobalColor range:range1];
        [attStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#333333"] range:range2];
        [attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, attStr.length)];
        self.currentYPLabel.attributedText = attStr;
        
        [_currentYPLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.exceptionalBtn4.mas_bottom).offset(110*K_SCALE_FULL);
            make.centerX.equalTo(self.view);
        }];
    }
    return _currentYPLabel;
}

-(UIButton *)rewardImmediatelyBtn
{
    if(_rewardImmediatelyBtn == nil)
    {
        _rewardImmediatelyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rewardImmediatelyBtn setBackgroundImage:[UIImage imageNamed:@"normal"] forState:UIControlStateNormal];
        [_rewardImmediatelyBtn setTitle:@"立刻答谢" forState:UIControlStateNormal];
        _rewardImmediatelyBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_rewardImmediatelyBtn setBackgroundImage:[UIImage imageNamed:@"highLight"] forState:UIControlStateHighlighted];
        [self.view addSubview:_rewardImmediatelyBtn];
        [_rewardImmediatelyBtn addTarget:self action:@selector(rewardImmediatelyBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
        [_rewardImmediatelyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.top.equalTo(self.currentYPLabel.mas_bottom).offset(15);
            make.left.equalTo(self.view).offset(25);
            make.right.equalTo(self.view).offset(-25);
        }];
    }
    return _rewardImmediatelyBtn;
}

-(void)loadUserCoinInfomation
{
    NSString *url = @"user/findUser.do";
    
    [[WPNetWorkManager sharedNetWorkManager] POST:url parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSString *str = nil;
        NSRange range1;
        
        if ([responseObject[@"result"] isEqualToString:@"success"])
        {
            NSDictionary *detail = responseObject[@"detail"];
            self.currentYPNum = [detail[@"gold"] intValue];
            str = [NSString stringWithFormat:@"我的医票:  %zd",self.currentYPNum];
            range1 = [str rangeOfString:[NSString stringWithFormat:@"%zd",self.currentYPNum]];
        }
        else
        {
            str = [NSString stringWithFormat:@"我的医票:  %zd",@"0"];
            range1 = [str rangeOfString:@"0"];
            
            //考虑到账号过期的情况
            if ([responseObject[@"detail"] isEqualToString:@"illegalLogin"])
            {
                //账号过期
                [self illegalLoginWithSuccessBlock:^{
                    
                    WPMineLoginVC *vc = [WPMineLoginVC new];
                    [self presentViewController:[[UINavigationController alloc] initWithRootViewController:vc] animated:YES completion:nil];
                }];
            }
            else
            {
                [self showErrorInfoWithStatus:@"获取失败"];
            }
        }

        NSRange range2 = [str rangeOfString:@"我的医票:"];
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:str];
        [attStr addAttribute:NSForegroundColorAttributeName value:GlobalColor range:range1];
        [attStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#333333"] range:range2];
        [attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, attStr.length)];
        self.currentYPLabel.attributedText = attStr;
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self showErrorInfoWithStatus:@"网络错误"];
    }];
    
}


-(WPBtnNoHighLight *)btnWith:(int)YPCount title:(NSString *)title isLast:(BOOL)isLast
{
    WPBtnNoHighLight *btn = [WPBtnNoHighLight buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:[UIImage imageNamed:@"exceptionalBtn_normal"] forState:UIControlStateNormal];
    [btn setBackgroundImage:[UIImage imageNamed:@"exceptionalBtn_selected"] forState:UIControlStateSelected];
    if (isLast)
    {
        [btn setTitle:title forState:UIControlStateNormal];
    }
    else
    {
        [btn setTitle:[NSString stringWithFormat:@"%zd医票",YPCount] forState:UIControlStateNormal];
    }
    [btn setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [self.view addSubview:btn];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn addTarget:self action:@selector(exceptionalBtnDidClick:) forControlEvents:UIControlEventTouchDown];
    return btn;
}

-(void)showCustomYBView
{
    UIButton *coverBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    coverBtn.backgroundColor = RGBA_COLOR(51, 51, 51, 0.3);
    [self.view addSubview:coverBtn];
    [coverBtn addTarget:self action:@selector(coverBtnDidClick:) forControlEvents:UIControlEventTouchDown];
    self.coverBtn = coverBtn;
    coverBtn.frame = self.view.bounds;
    
    UIView *customYPView = [UIView new];
    customYPView.backgroundColor = RGB_SAME(250);
    customYPView.layer.cornerRadius = 10;
    customYPView.layer.masksToBounds = YES;
    [coverBtn addSubview:customYPView];
    self.customYPView = customYPView;
    [customYPView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(coverBtn).offset(179*K_SCALE_FULL);
        make.left.equalTo(coverBtn).offset(32);
        make.right.equalTo(coverBtn).offset(-32);
        make.height.mas_equalTo(195);
    }];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"其它医票";
    titleLabel.textColor = GLobalFontColor;
    titleLabel.font = [UIFont systemFontOfSize:15];
    [customYPView addSubview:titleLabel];
    CGSize size=  [titleLabel.text sizeWithMaxSize:CGSizeMake(MAXFLOAT, MAXFLOAT) font:15];
    CGFloat margin = (45-size.height)*0.5;
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(customYPView);
        make.top.equalTo(customYPView).offset(margin);
    }];
    
    UIImageView *lineView = [UIImageView lineImageView];
    [customYPView addSubview:lineView];
    lineView.frame = CGRectMake(0, 45, SCREEN_WIDTH-2*32, 0.5);
    
    UIView *bgView = [UIView new];
    bgView.backgroundColor = [UIColor whiteColor];
    [customYPView addSubview:bgView];
    bgView.layer.cornerRadius = 4;
    bgView.layer.masksToBounds = YES;
    bgView.layer.borderWidth = 0.5;
    bgView.layer.borderColor = GLobalLineColor.CGColor;
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lineView).offset(22);
        make.left.equalTo(customYPView).offset(18);
        make.right.equalTo(customYPView).offset(-18);
        make.height.mas_equalTo(44);
    }];


    UILabel *leftLabel = [UILabel new];
    leftLabel.text = @"医票数量";
    leftLabel.textColor = GLobalFontColor;
    leftLabel.font = [UIFont systemFontOfSize:14];
    [bgView addSubview:leftLabel];
    CGSize leftLabelSize = [leftLabel.text sizeWithMaxSize:CGSizeMake(MAXFLOAT, MAXFLOAT) font:14];
    [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView).offset(15);
        make.centerY.equalTo(bgView);
        make.width.mas_equalTo(leftLabelSize.width+2);
    }];
    
    UITextField *fieldView = [UITextField new];
    fieldView.placeholder = @"请输入答谢的医票数量";
    fieldView.font = [UIFont systemFontOfSize:14];
    self.textField = fieldView;
    fieldView.delegate = self;
    [fieldView addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [bgView addSubview:fieldView];
    fieldView.keyboardType = UIKeyboardTypeNumberPad;
    [fieldView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftLabel.mas_right).offset(15);
        make.right.equalTo(bgView);
        make.centerY.equalTo(bgView);
    }];
    
    UIButton *dashanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [dashanBtn setBackgroundImage:[UIImage imageNamed:@"normal"] forState:UIControlStateNormal];
    [dashanBtn setBackgroundImage:[UIImage imageNamed:@"highLight"] forState:UIControlStateHighlighted];
    [dashanBtn setTitle:@"答谢" forState:UIControlStateNormal];
    self.dashanBtn = dashanBtn;
    dashanBtn.enabled = NO;
    [customYPView addSubview:dashanBtn];
    dashanBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [dashanBtn addTarget:self action:@selector(rewardImmediatelyBtnDidClick) forControlEvents:UIControlEventTouchUpInside];
    [dashanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(bgView);
        make.top.equalTo(bgView.mas_bottom).offset(22);
    }];
    
    [self exChangeOut:customYPView dur:0.1];
}

// 键盘弹出时调用方法
-(void)keyboardDidShow:(NSNotification *)notification
{
    //获取键盘的高度
    NSDictionary *userInfo = [notification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    
    CGRect rect = [self.coverBtn convertRect:self.customYPView.frame toView:[UIApplication sharedApplication].keyWindow];
    
    if ((keyboardRect.origin.y-rect.origin.y) <= self.customYPView.bounds.size.height)
    {
        // 取得键盘的动画时间，这样可以在视图上移的时候更连贯
        double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

        [UIView animateWithDuration:duration animations:^{
            
            self.customYPView.frame = CGRectMake(32, keyboardRect.origin.y-self.customYPView.bounds.size.height-15, SCREEN_WIDTH-64, 195);
        }];
    }
}

//当键盘出现或改变时调用
-(void)keyboardWillHide:(NSNotification *)aNotification
{
    // 取得键盘的动画时间，这样可以在视图上移的时候更连贯
    double duration = [[aNotification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    
    [UIView animateWithDuration:duration animations:^{
        
        self.customYPView.frame = CGRectMake(32, 179*K_SCALE_FULL, SCREEN_WIDTH-64, 195);
    }];
}

-(void)exChangeOut:(UIView *)changeOutView dur:(CFTimeInterval)dur{
    
    CAKeyframeAnimation * animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    
    animation.duration = dur;
    
    //animation.delegate = self;
    
    animation.removedOnCompletion = NO;
    
    animation.fillMode = kCAFillModeForwards;
    
    NSMutableArray *values = [NSMutableArray array];
    
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1.0)]];
     [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    
    animation.values = values;
    animation.keyTimes = @[@0.0f, @0.5f, @0.75f, @1.0f];
    
    animation.timingFunction = [CAMediaTimingFunction functionWithName: @"easeInEaseOut"];
    
    [changeOutView.layer addAnimation:animation forKey:nil];
    
}

#pragma mark - UITextView的代理方法
-(void)textFieldDidChange:(UITextField *)textField
{
    self.rewardYPNum = [textField.text integerValue];
    
    if (textField.text.length)
    {
        self.dashanBtn.enabled = YES;
    }
    else
    {
        self.dashanBtn.enabled = NO;
    }

}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    if (toBeString.length > kMaxLength && range.length!=1){
        textField.text = [toBeString substringToIndex:kMaxLength];
        return NO;
    }
    return YES;
}

#pragma mark - 按钮点击事件
-(void)exceptionalBtnDidClick:(WPBtnNoHighLight *)btn
{
   if(btn == self.currentSelectedBtn)
   {
       return ;
   }
    
    if (btn.tag == 1005)
    {
        //其他一票
        [self showCustomYBView];
        return ;
    }
    
    self.currentSelectedBtn.selected = NO;
    self.currentSelectedBtn = btn;
    self.currentSelectedBtn.selected = YES;
    
    if (btn.tag == 1000)
    {
        self.rewardYPNum = 5;
    }
    else if (btn.tag == 1001)
    {
        self.rewardYPNum = 10;
    }
    else if (btn.tag == 1002)
    {
        self.rewardYPNum = 15;
    }
    else if (btn.tag == 1003)
    {
        self.rewardYPNum = 20;
    }
    else if (btn.tag == 1004)
    {
        self.rewardYPNum = 50;
    }

}

-(void)coverBtnDidClick:(UIButton *)btn
{
    [btn removeFromSuperview];
}

-(void)rewardImmediatelyBtnDidClick
{
    
    [self.textField resignFirstResponder];
    
    //医票不足
    if(self.rewardYPNum == 0)
    {
        [self showInfoStatus:@"医票数必须大于0"];
    }
    else if (self.rewardYPNum > self.currentYPNum)
    {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"您的医票数量不足,是否去充值" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"充值" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            //        调到购买金币界面去购买金币
            self.hidesBottomBarWhenPushed = YES;
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"WPGoldCoinVC" bundle:nil];
            WPCoinTopUp *gcVC = [sb instantiateViewControllerWithIdentifier:@"goldTopUp"];
            [self.navigationController pushViewController:gcVC animated:YES];
            self.hidesBottomBarWhenPushed = NO;
            
        }];
        [alertController addAction:sureAction];
        [alertController addAction:cancelAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }
    else
    {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:[NSString stringWithFormat:@"您确定花费%zd医票答谢该份病例吗?",self.rewardYPNum] preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self showWithStatus:@"正在答谢"];
            
            NSString *URL= @"user/reward.do";
            NSDictionary *dict = @{@"recordId":@(self.model.id),@"gold":@(self.rewardYPNum)};
            
            [[WPNetWorkManager sharedNetWorkManager] POST:URL parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
                
            } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                
                [self dismiss];
                
                if([responseObject[@"detail"] isEqualToString:@"success"])
                {
                    [self showSuccessInfoWithStatus:@"成功答谢"];
                    [self backBtnDidClick];
                }
                else
                {
                    //            考虑到账号过期的情况
                    if ([responseObject[@"detail"] isEqualToString:@"illegalLogin"])
                    {
                        //                账号过期
                        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"账号过期,请重新登录" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
                        [alertView show];
                    }
                    else
                    {
                        [self showErrorInfoWithStatus:@"服务器错误"];
                    }
                }
            } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                
                [self dismiss];
                [self showErrorInfoWithStatus:@"网络错误"];
            }];

            
        }];
        [alertController addAction:cancel];
        [alertController addAction:sure];
        
        [self presentViewController:alertController animated:YES completion:nil];
        
    }
}

-(void)backBtnDidClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
